using System;
using System.Collections;
using UnityEngine;

public class PlanLost : Plan
{
	private AgentBehaviourScript agent;
	private Maze maze;
	private pathplanner pathPlanner = null;
	private Plan currentPlan = null;
	private Vector3 destination;
	private ArrayList obstacles = null;
	
	private int maxNodes = 8000;
	private int nodesPerCycle; // number of nodes to open per frame
	
	private bool pathComplete = false;
	
	public PlanLost(AgentBehaviourScript agent, Maze maze)
	{
		this.maze = maze;
		this.agent = agent;
	}
	
	public override bool execute ()
	{
		if ( currentPlan == null || !currentPlan.execute() )
		{
			// no currentPlan or currentPlan finished
			while(true)
			{
				float x = UnityEngine.Random.value * (maze.getWidth() * maze.getCellSize());	
				float y = UnityEngine.Random.value * (maze.getWidth() * maze.getCellSize());	
				
				MazeLocation destination = new MazeLocation((int)x,(int)y); 
				if(maze.getMazeSquare(destination).isSolid() == false)
				{
					Vector3 pos = maze.getMazePosition(destination);
					currentPlan = new PlanPathFindToPosition(agent, maze, pos);
					break;
				} 
			}
		}

		
		return true;
	}
	
	
	public override pathplanner getPathPlanner() {
		if (currentPlan	!= null) {
			return currentPlan.getPathPlanner();
		} else {
			return null;
		}
	}

	public override string ToString()
	{
		string returnString = string.Format ("Getting Lost( ");
		if (currentPlan != null)
			returnString += currentPlan.ToString();
		returnString += " )";
		return returnString;
	}
	
	
}

